﻿using EP.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EP.Services.TodoItem
{
    public class TodoItemService : ITodoItemService
    {
        private const string MODEL_KEY = "EP.Services.TodoItem";
        private IMemoryCache _memoryCache;
        private IRepository<Entities.TodoItem> _todoItemRepository;
        public TodoItemService(IRepository<Entities.TodoItem> todoItemRepository, IMemoryCache memoryCache)
        {
            this._memoryCache = memoryCache;
            this._todoItemRepository = todoItemRepository;
        }
        public void DeleteTodoItem(long id)
        {
            var item = GetTodoItem(id);
            if(item != null)
            {
                _todoItemRepository.Delete(item);
                RemoveCache();
            }
        }
        public Entities.TodoItem GetTodoItem(long id)
        {
            return _todoItemRepository.GetById(id);
        }
        public List<Entities.TodoItem> GetTodoItems()
        {
            List<Entities.TodoItem> list = null;
            _memoryCache.TryGetValue<List<Entities.TodoItem>>(MODEL_KEY, out list);
            if (list != null) return list;
            list = _todoItemRepository.Table.ToList();
            _memoryCache.Set(MODEL_KEY, list, DateTimeOffset.Now.AddDays(1));
            return list;
        }
        public void InsertTodoItem(Entities.TodoItem todoItem)
        {
            _todoItemRepository.Insert(todoItem);
            RemoveCache();
        }
        public void UpdateTodoItem(Entities.TodoItem todoItem)
        {
            _todoItemRepository.Update(todoItem);
            RemoveCache();
        }
        public void RemoveCache()
        {
            _memoryCache.Remove(MODEL_KEY);
        }

        public ValueTask<Entities.TodoItem> GetTodoItemAsync(long id)
        {
            return _todoItemRepository.GetByIdAsync(id);
        }

        public Task<List<Entities.TodoItem>> GetTodoItemsAsync()
        {
            // List<Entities.TodoItem> list = null;
            // _memoryCache.TryGetValue<List<Entities.TodoItem>>(MODEL_KEY, out list);
            // if (list != null) return list;
            // list = _todoItemRepository.Table.ToList();
            // _memoryCache.Set(MODEL_KEY, list, DateTimeOffset.Now.AddDays(1));
            // return list;

            return _todoItemRepository.Entities.ToListAsync();
        }

        public Task InsertTodoItemAsync(Entities.TodoItem todoItem)
        {
            var task = _todoItemRepository.InsertAsync(todoItem);
            RemoveCache();
            return task;
        }

        public Task DeleteTodoItemAsync(long id)
        {
            var getTask = GetTodoItemAsync(id).AsTask();
            Task.WhenAll(getTask);
            var item = getTask.Result;
            if(item != null)
            {
                var deleteTask = _todoItemRepository.DeleteAsync(item);
                RemoveCache();
                return deleteTask;
            }
            return new Task(()=>{});
        }

        public Task UpdateTodoItemAsync(Entities.TodoItem todoItem)
        {
            var task = _todoItemRepository.UpdateAsync(todoItem);
            RemoveCache();
            return task;
        }
    }
}
